/*
 * @Descripttion: 
 * @version: 
 * @@Company: DCIT-SH
 * @Author: SuPeiSen
 * @Date: 2020-01-10 17:30:42
 * @LastEditors  : SuPeiSen
 * @LastEditTime : 2020-01-13 16:09:30
 */

/// https://www.jianshu.com/p/3995ca566d9b
/// https://www.jianshu.com/p/c94326208c27

import 'package:flutter/material.dart';
import 'package:love_app/common/su.dart';
import 'package:love_app/db/db.dart';
import 'package:sqflite/sqflite.dart';

abstract class BaseDb{

  bool isTableExits = false; // 表是否存在

  createTableString(); // 数据表创建语句

  tableName(); // 数据表名

  ///创建表sql语句
  tableBaseString(String sql) {
    return sql;
  }

  // 这个@mustCallSuper好像是标识子类会自动初始化该方法
  @mustCallSuper
  Future prepare(String tableName, String createSql)async{
    isTableExits = await MySqfite.isTableExits(tableName);
    if(isTableExits == false){
      Database db = await MySqfite.getCurrentDatabase();
      return await db.execute(createSql);
    }
  }

  @mustCallSuper
  Future open()async{
    if(isTableExits == false){
      await prepare(tableName(), createTableString());
    }
    return await MySqfite.getCurrentDatabase();
  }

  Future<Database> getDataBase() async {
    return await open();
  }
  
}

